<template>
 <div class="pagination">

     <button :disabled="pageNo==1" @click="$emit('getPageNo',pageNo-1)">上一页</button>
    <button v-if="startNumber.start >1" @click="$emit('getPageNo',1)" :class="{active:pageNo==1}">1</button>
    <button v-if="startNumber.start>2">···</button>

    <button v-for="(page,index) in startNumber.end" :key="index" v-if="page>=startNumber.start" @click="$emit('getPageNo',page)" :class="{active:pageNo==page}">{{page}}</button>
    
    
    <button v-if="startNumber.end<GeneralPage-1">···</button>
    <button v-if="startNumber.end<GeneralPage"  @click="$emit('getPageNo',GeneralPage)" :class="{active:pageNo==GeneralPage}" >{{GeneralPage}}</button>
    <button  @click="$emit('getPageNo',pageNo+1)" :disabled="pageNo==GeneralPage">下一页</button>
    
    <button style="margin-left: 30px">共 {{total}} 条</button>
  </div>
</template>

<script>
          //  pageNo="1" 表示当前页数 
          //  :pageSize="3"  展示数据几条
          //  :total="91"    一共多少数据
          //  :continues="5"/>连续页数展示
         
export default {
  name: "Pagination",
  props:['pageNo','pageSize','total','continues'],
  computed:{  
     startNumber(){  
       const {continues,pageNo,GeneralPage}=this
       
      let start=0,end=0;
      if(continues>GeneralPage){//判断连续页是否超过总页  GeneralPage不能少于continues 
        start=1;
        end=GeneralPage
      }else{
        start=pageNo-parseInt(continues/2)  //求出连续页起始页  假设当前为6 连续页是5   用6-(5/2)   为4  parseInt取整
        end=pageNo+parseInt(continues/2) //求出连续页最后一个  假设当前为6 连续页是5   用6+(5/2)   为8  parseInt取整
      }                                           //那么连续页为4 5 6 7 8   当前页在中间
      if(start<1){  //假设 起始页为1  出现负页的情况   防止出现 -1 0 1 2 3  
        start=1;
        end=continues  
      } 
      // 假如最后页大于30  防止出现28 29 30 31 32   
      if(end>GeneralPage){end=GeneralPage;start=GeneralPage-continues+1}  
      return {start,end}
     },
     GeneralPage(){
       return Math.ceil(this.total/this.pageSize)   // 总数据除以一页所展示的数据  90/3=30页
     },
    
  },
  methods:{
     passPage(){
       this.$emit("getPageNo",this.pageNo)
     }
  }
 
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
.active{
  background: skyblue;
}
</style>
